package com.example.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.adapter.RvAdapter;
import com.example.studyapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:复用缓存机制
 * @Author: 葛浩杰
 * @Version: 1.0 2021/1/28
 */
public class RecyclerViewActivity extends Activity {

    private RecyclerView recyclerView;
    private RecyclerView recyclerView2;
    private RvAdapter rvAdapter;
    private RvAdapter rvAdapter2;

    public static void startActivity(Context context){
        Intent intent = new Intent(context, RecyclerViewActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv);

        recyclerView = findViewById(R.id.rv_recycle);
        recyclerView2 = findViewById(R.id.rv_recycle2);

        initData();

    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView2.setLayoutManager(new LinearLayoutManager(this));
        rvAdapter = new RvAdapter(this, getData());
        rvAdapter2 = new RvAdapter(this, getData2());
        recyclerView.setAdapter(rvAdapter);
        recyclerView2.setAdapter(rvAdapter2);


        recyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {
            @Override
            public boolean onFling(int velocityX, int velocityY) {
                Log.e("ghjj", velocityY +"");
                return false;
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private List<String> getData() {
        List<String> rvData = new ArrayList<>();
        for(int i=0;i<40;i++){
            rvData.add("缓存复用--" + i);
        }
        return rvData;
    }

    private List<String> getData2() {
        List<String> rvData = new ArrayList<>();
        for(int i=0;i<40;i++){
            rvData.add("底部list--" + i);
        }
        return rvData;
    }


}
